package dyyx.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public abstract class WebUtil {
	
	private static volatile String  webRootDir = null;
	private static volatile String  contextPath = null;

	
			
   
	
	public synchronized static void setWebRootDir(String dir){
		if(StringUtils.isBlank(dir)){
			throw new RuntimeException("dir is null");
		}
		if(webRootDir!=null){
			return;
		}
		
	    webRootDir = dir;
	}
	
	
	public static String getWebRootDir(){
		if(webRootDir==null){
			throw new RuntimeException("webRootDir is null");
		}
		
		
		
		return  webRootDir;
	}
	
	public synchronized static void setContextPath(String path){
		// maybe ""
		if(path==null){
			throw new RuntimeException("path is null");
		}
		if(contextPath!=null){
			return;
		}
		
		contextPath = path;
	}
	
	
	public static String getContextPath(){
		if(contextPath==null){
			throw new RuntimeException("contextPath is null");
		}
		
		
		
		return  contextPath;
	}
	

   public static String getPath(String path){
	   if(path==null){
		   return path;
	   }
	   if(contextPath==null || contextPath.isEmpty()){
		   return path;
	   }
	   if(path.startsWith("/")){
		   return contextPath+path;
	   }
	   return path;
   }

	
	
	public static String get(HttpServletRequest request, String name) {
		if (request == null || name == null) {
			return null;
		}
		String str = request.getParameter(name);
		if (str != null) {
			str = str.trim();
		}
		return str;
	}

	public static int getInt(HttpServletRequest request, String name) {
		String str = get(request, name);
		return CommUtil.getInt(str);
	}
	

	public static int getInt(HttpServletRequest request, String name,int def) {
		String str = get(request, name);
		return CommUtil.getInt(str,def);
	}

	public static long getLong(HttpServletRequest request, String name) {
		String str = get(request, name);
		return CommUtil.getLong(str);
	}
	

	public static long getLong(HttpServletRequest request, String name,long def) {
		String str = get(request, name);
		return CommUtil.getLong(str,def);
	}
	
	public static double getDouble(HttpServletRequest request, String name) {
		String str = get(request, name);
		return CommUtil.getDouble(str);
	}
	

	public static double getDouble(HttpServletRequest request, String name,int def) {
		String str = get(request, name);
		return CommUtil.getDouble(str,def);
	}
	
	public static String toJsonString(Object obj){
		if(obj==null){
			return "";
		}
		return JSON.toJSONString(obj, SerializerFeature.PrettyFormat,SerializerFeature.DisableCircularReferenceDetect);
	}
	
	
	public static boolean isError(String str){
		if(str==null){
			return false;
		}
		
		String str2 = str.toLowerCase();
		if(str2.indexOf("error")>=0 || str2.indexOf("exception")>=0){
			return true;
		}
		return false;
	}
	
	public static String showInfo(String str){
		if(str==null){
			str = "";
		}
		StringBuilder sb = new StringBuilder(128);
		
		//sb.append("<div>");
		if(isError(str)){
			sb.append("<span class='error'>");
		}else{
			sb.append("<span>");
		}
		sb.append(str);
		
		sb.append("</span>");	
		
		return sb.toString();
	}

}
